<!--  -->
<template>
  <el-container class="home-container">
    <!-- 导航标题 -->
    <el-header class="home-header">
      <div>
        <img src="../assets/images/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏菜单区域 -->
      <el-aside :width="fold?'64px':'200px'">
        <div class="fold-strip" @click="fold=!fold">||||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="fold"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="iconFont[item.id]"></i>
              <!-- 一级菜单标题 -->
              <span v-text="item.authName"></span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+subItem.path)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 二级菜单图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单标题 -->
                <span v-text="subItem.authName"></span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconFont: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      fold: false,
      activePath: '/users' // 默认被激活的地址
    }
  },

  created() {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  },

  methods: {
    // 登录注销
    logout() {
      // 1、清除token
      sessionStorage.removeItem('token')
      // 2、跳转登录页面
      this.$router.push('/login')
    },
    // 侧边栏菜单获取
    getMenuList() {
      this.axios.get('menus').then(resp => {
        if (resp.meta.status === 200) {
          this.menuList = resp.data
        } else {
          this.$message.error('获取侧边栏菜单列表失败')
        }
      })
    },
    // 保存连接的激活状态
    saveNavState(activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },

  components: {}
}
</script>

<style lang='scss' scoped>
.home-container {
  height: 100%;
  .home-header {
    background: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    div {
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        font-size: 20px;
        color: #fff;
        margin-left: 10px;
      }
    }
  }
  .el-container {
    user-select: none;
    .el-aside {
      background-color: #333744;
      .fold-strip {
        font-size: 16px;
        line-height: 24px;
        color: #fff;
        background: #4a5064;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
      }
      .el-menu {
        border-right: none;
      }
      .iconfont {
        margin-right: 5px;
      }
    }
    .el-main {
      background-color: #eaedf1;
    }
  }
}
</style>
